﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.Shell;
using KeelKit.Serialization;
using EnvDTE;

namespace KeelKit.KeelKit_CodeLineCounter
{
    public class Common
    {
        public static OutputWindowPane chOutWin;
        public static EnvDTE80.DTE2 chDTE
        {
            get
            {
                return (EnvDTE80.DTE2)Package.GetGlobalService(typeof(EnvDTE.DTE));
            }
        }

        /// <summary> 
        /// 在输出窗口和状态条中显示文本 
        /// </summary> 
        /// <param name="Text">为要输出的文本内容</param> 
        /// <param name="cCrlf">决定是不是要换行,默认为换行</param> 
        /// <param name="bMustOut">决定该输出是不是必须输出的.</param> 
        /// <remarks>如果不是重要的信息, 用户的不显示详细信息设置将过滤该输出信息</remarks> 
        public static void ShowInfo(string Text, bool notime)
        {
            try
            {
                if (Common.chOutWin == null)
                {
                    try
                    {
                        Common.chOutWin = Common.chDTE.ToolWindows.OutputWindow.OutputWindowPanes.Item("KeelKit");
                    }
                    catch (Exception)
                    {
                        Common.chOutWin = Common.chDTE.ToolWindows.OutputWindow.OutputWindowPanes.Add("KeelKit");
                    }
                }
                chOutWin.OutputString((notime ? "" : DateTime.Now.ToString("yyyyMMddHHmmss :")) + Text + System.Environment.NewLine);
                chDTE.StatusBar.Text = "KeelKit::" + Text;
            }
            catch (Exception)
            {

            }
        }
        public static void ShowInfo(string Text)
        {
            ShowInfo(Text, false);
        }
        public static void InitEvents()
        {
            try
            {
                System.Windows.Forms.Application.DoEvents();
                chDTE.Events.SolutionEvents.Opened += new EnvDTE._dispSolutionEvents_OpenedEventHandler(SolutionEvents_Opened);
                System.Windows.Forms.Application.DoEvents();
                chDTE.Events.SolutionEvents.BeforeClosing += new EnvDTE._dispSolutionEvents_BeforeClosingEventHandler(SolutionEvents_BeforeClosing);
                System.Windows.Forms.Application.DoEvents();
            }
            catch (Exception)
            {


            }
        }
        public static ctlCLC clc = null;
        /// <summary>
        /// 
        /// </summary>
        static void SolutionEvents_BeforeClosing()
        {
            if (clc != null)
            {
                try
                {
                    clc.Save();
                }
                catch (Exception)
                {

                    ;
                }
            }
        }

        static void SolutionEvents_Opened()
        {
            if (clc != null)
            {

                try
                {
                    clc.Init();
                }
                catch (Exception)
                {


                }
            }
        }

    }
}
